package com.wanglin.mqttandroidclient

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.wanglin.mqttandroidclient.databinding.ActivityMainBinding
import java.text.SimpleDateFormat
import java.util.*

class MainActivity : AppCompatActivity() {
    private lateinit var mBinding: ActivityMainBinding
    companion object{
        private const val TAG = "MainActivity"
    }

    @SuppressLint("SimpleDateFormat", "SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(mBinding.root)

        //接收到订阅的消息显示出来
        MyApplication.instance.setOnDismissListener {
            Log.d("msg=",it.toString())
            val sdf=  SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
           val now= sdf.format(Date())
            runOnUiThread {
                mBinding.tvSub.text="${mBinding.tvSub.text.toString()}\n${now}->$it"
            }

        }

        //发布消息
        mBinding.btnPub.setOnClickListener {
            val pubMsg= mBinding.etPubMsg.text.toString()
            MyApplication.instance.publishMessage(pubMsg)
        }



    }


}